package com.qf.dao.impl;

import com.qf.bean.Student;
import com.qf.bean.Users;
import com.qf.dao.UsersDao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * 2019/11/13
 * Administrator
 * spring_anno_aop_1113
 * 面向对象面向君  不负代码不负卿
 */
@Repository
public class UsersDaoImpl extends JdbcDaoSupport implements UsersDao {
    public void a() {
        System.out.println("usersDaoImpl()-a()");
        System.out.println(5/0);
    }

    public int insert(String name, String sex) {

        return getJdbcTemplate().update("insert into student(sname2,sex) values(?,?)",name,sex);
    }

    public List<Student> findall(String name) {
        List list=
        getJdbcTemplate().query("select * from student", new RowMapper<Student>() {
            public Student mapRow(ResultSet resultSet, int i) throws SQLException {
                System.out.println("maprow"+i);
                Student student = new Student();
                student.setSid(resultSet.getInt("sid"));
                student.setSname2(resultSet.getString("sname2"));
                return student;
            }
        });
        return list;
    }

    public Student findbyid(int id) {
        Student s= getJdbcTemplate().queryForObject("select * from student where sid=?", new RowMapper<Student>() {
            public Student mapRow(ResultSet resultSet, int i) throws SQLException {
                Student student = new Student();
                student.setSid(resultSet.getInt("sid"));
                student.setSname2(resultSet.getString("sname2"));
                return student;
            }
        },id);

        return s;
    }

    public int count() {
        Integer integer = getJdbcTemplate().queryForObject("select count(*) from student", int.class);
        return integer;
    }

    public Map getmap() {
        Map map=
        getJdbcTemplate().queryForMap("select count(*) count,max(sid),min(sid),avg(sid),sum(sid) from student");
        return map;
    }
}
